package com.fujitsu.sodoku;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener,
		DialogInterface.OnClickListener {
	private final String TAG = "sodoku";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button aboutBtn = (Button) findViewById(R.id.about_game);
		Button continueBtn = (Button) findViewById(R.id.continue_game);
		Button newBtn = (Button) findViewById(R.id.new_game);
		Button exitBtn = (Button) findViewById(R.id.exit_game);
		SharedPreferences preference = getPreferences(Context.MODE_PRIVATE);
		boolean isPaused = preference.getBoolean(GameActivity.isPaused, false);
		aboutBtn.setOnClickListener(this);
		continueBtn.setOnClickListener(this);
		newBtn.setOnClickListener(this);
		exitBtn.setOnClickListener(this);
		if (!isPaused) {
			continueBtn.setEnabled(false);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.continue_game:
			break;
		case R.id.new_game:
			new AlertDialog.Builder(this).setTitle(R.string.difficulty)
					.setItems(R.array.difficulty, this).show();
			break;
		case R.id.about_game:
			Intent intent = new Intent(this, AboutActivity.class);
			startActivity(intent);
			break;

		case R.id.exit_game:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:
			startActivityForResult((new Intent(this, SettingActivity.class)), 0);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		startGame(which);

	}

	private void startGame(int dificulty) {
		Log.i(TAG,
				this.getResources().getStringArray(R.array.difficulty)[dificulty]);
		Intent gameIntent = new Intent(this, GameActivity.class);
		gameIntent.putExtra(GameActivity.Game_Difficulty, dificulty);
		startActivity(gameIntent);

	}
}
